/**
 * OA项目资源存储
 * 管理系统全局资源和缓存
 */

const mobx = require('../../../../../../miniprogram_npm/mobx/index');
const { observable, action, runInAction } = mobx;

// 缓存配置
const CACHE_GROUP = {
  CACHE_KEY: 'RESOURCES_CACHE_KEY',
  CACHE_TIME: 30 * 60 * 1000 // 30分钟
};

/**
 * 资源存储
 * 管理系统公共资源
 */
class ResourcesStore {
  // 状态定义
  loading = false;
  error = null;
  resourceList = [];
  currentResource = null;
  filter = { status: 'all', keyword: '' };
  pagination = { page: 1, size: 10, total: 0, hasMore: true };

  // 缓存配置
  CACHE_GROUP = CACHE_GROUP;
  
  /**
   * 构造函数
   */
  constructor() {
    console.log('[resourcesStore] 初始化');
  }
  
  /**
   * 重置状态
   */
  reset() {
    console.log('[resourcesStore] 重置状态');
    this.loading = false;
    this.error = null;
    this.resourceList = [];
    this.currentResource = null;
    this.filter = { status: 'all', keyword: '' };
    this.pagination = { page: 1, size: 10, total: 0, hasMore: true };
  }
  
  /**
   * 设置加载状态
   * @param {boolean} loading 加载状态
   */
  setLoading(loading) {
    this.loading = loading;
  }
  
  /**
   * 设置错误信息
   * @param {*} error 错误信息
   */
  setError(error) {
    this.error = error;
  }
  
  /**
   * 设置资源列表
   * @param {Array} list 资源列表
   */
  setResourceList(list) {
    this.resourceList = list || [];
  }
  
  /**
   * 设置当前资源
   * @param {Object} resource 资源对象
   */
  setCurrentResource(resource) {
    this.currentResource = resource;
  }
  
  /**
   * 加载资源列表
   * @param {Object} params 请求参数
   * @returns {Promise} 请求Promise
   */
  async loadResourceList(params = {}) {
    console.log('[resourcesStore] 加载资源列表', params);
    this.setLoading(true);
    this.setError(null);
    
    try {
      // 模拟加载数据
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 模拟数据
      const mockData = Array(10).fill(0).map((_, i) => ({
        id: `resource-${i}`,
        name: `资源 ${i+1}`,
        type: i % 3 === 0 ? 'file' : 'folder',
        size: i * 1024,
        lastModified: new Date().toISOString()
      }));
      
      runInAction(() => {
        this.resourceList = mockData;
        this.pagination = {
          page: 1,
          size: 10,
          total: mockData.length,
          hasMore: false
        };
        this.loading = false;
      });
      
      return mockData;
    } catch (error) {
      runInAction(() => {
        this.loading = false;
        this.error = error.message || '加载资源失败';
      });
      throw error;
    }
  }
  
  /**
   * 加载资源详情
   * @param {string} id 资源ID
   * @returns {Promise} 请求Promise
   */
  async loadResourceDetail(id) {
    console.log('[resourcesStore] 加载资源详情', id);
    this.setLoading(true);
    this.setError(null);
    
    try {
      // 模拟加载数据
      await new Promise(resolve => setTimeout(resolve, 300));
      
      // 模拟数据
      const detail = {
        id,
        name: `资源 ${id}`,
        type: 'file',
        size: 10240,
        createTime: new Date().toISOString(),
        lastModified: new Date().toISOString(),
        url: 'https://example.com/resource',
        metadata: {
          description: '资源描述信息',
          tags: ['文档', '重要']
        }
      };
      
      runInAction(() => {
        this.currentResource = detail;
        this.loading = false;
      });
      
      return detail;
    } catch (error) {
      runInAction(() => {
        this.loading = false;
        this.error = error.message || '加载资源详情失败';
      });
      throw error;
    }
  }
  
  /**
   * 初始化方法
   * @returns {this} 当前实例
   */
  init() {
    console.log('[resourcesStore] 初始化完成');
    return this;
  }
}

// 使用mobx.decorate代替装饰器语法
mobx.decorate(ResourcesStore, {
  // 可观察属性
  loading: observable,
  error: observable,
  resourceList: observable,
  currentResource: observable,
  filter: observable,
  pagination: observable,
  
  // Action方法
  reset: action,
  setLoading: action,
  setError: action,
  setResourceList: action,
  setCurrentResource: action,
  loadResourceList: action,
  loadResourceDetail: action
});

// 创建单例
const resourcesStore = new ResourcesStore();
module.exports = resourcesStore; 